﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
using System.Collections.ObjectModel;
using FieldTemplate;
using System.Reflection;

namespace WpfDevelopHelper
{
    /// <summary>
    /// OrderNumSet.xaml 的交互逻辑
    /// </summary>
    public partial class OrderNumSet : UserControl
    {
        public OrderNumSet()
        {
            InitializeComponent();
            //var vm = new OrderNumFieldSetVm();
            //vm.SetPanel = this;
            //this.DataContext = vm;

        }

    }

    [Serializable]
    public class TemplateModel
    {
        public string TemplateName { get; set; }//学生信息

        public string OutputTemplate;

        public List<Fieldinfo> fieldinfos;

        public ObservableCollection<FieldSetVmBase> GenerateFieldSetVm(List<FieldInfo> FieldTypes)
        {
            ObservableCollection<FieldSetVmBase> result = new ObservableCollection<FieldSetVmBase>();

            if (fieldinfos == null)
                return result;
            foreach (var field in fieldinfos)
            {
                FieldSetVmBase vm = null;
                //判断类型
               var tt= FieldTypes.FirstOrDefault(a => { return a.FieldName == field.FieldType; });
                if(tt==null)
                {
                    MessageBox.Show("不存在类型为" + field.FieldType + "的类型");
                    return result;
                }
                vm = tt.ReflactionCreateInstance() as FieldSetVmBase;

                vm.FieldName = field.FieldName;
                vm.FieldType = field.FieldType;

                if (field.propertyinfos != null)
                {
                    Type childtype = vm.GetType();
                    foreach (var pt in field.propertyinfos)
                    {
                        PropertyInfo ptinfo = childtype.GetProperty(pt.PorpertyName);

                        object value=null;
                        switch (pt.PropertyType)//int   string bool double
                        {
                            case "int":value = int.Parse(pt.PropertyValue);break;
                            case "double": value = double.Parse(pt.PropertyValue); break;
                            case "bool": value = bool.Parse(pt.PropertyValue); break;


                            default:
                                value = pt.PropertyValue;
                                break;
                        }
                        ptinfo.SetValue(vm, value);
                    }

                }
                result.Add(vm);
            }
            return result;
        }

    }

    public class OrderNumFieldSetVm : FieldSetVmBase
    {
        public OrderNumFieldSetVm()
        {
            FieldName = "学号";
            FieldType = "ordernum";
            StartNum = 0;
            Step = 1;


            SetPanel = new OrderNumSet();
            SetPanel.DataContext = this;

        }



        private int _StartNum;
        public int StartNum
        {
            get
            {
                return _StartNum;
            }
            set
            {
                _StartNum = value;
                RaisePropertyChanged("StartNum");
            }
        }


        private int _Step;
        public int Step
        {
            get
            {
                return _Step;
            }
            set
            {
                _Step = value;
                RaisePropertyChanged("Step");
            }
        }


        private int GetOrderNum()
        {
            StartNum += Step;
            return StartNum;
        }

        public override string GetFieldValue()
        {
            return GetOrderNum().ToString();
        }


        public override Fieldinfo Serialize()
        {
            var tmp = base.Serialize();
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "StartNum", PropertyType = "int", PropertyValue = StartNum.ToString() });
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "Step", PropertyType = "int", PropertyValue = Step.ToString() });

            return tmp;
        }
    }






}
